/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "commonheaders.h"
#include "m_popup.h"

#include "ctrl.h"
#include "Reminder.h"
#include "svcExImport.h"
#include "menuitems.h"
#include "dlgPropsheet.h"
#include "ExtraServices.h"

namespace NOptions {

#define		PSM_ENABLE_TABITEM	(WM_USER+106)

static VOID ClearAllExtraIcons(INT ColumnType)
{
	IconExtraColumn iec;
	HANDLE hContact;

	iec.cbSize = sizeof(IconExtraColumn);
	iec.hImage = INVALID_HANDLE_VALUE;
	iec.ColumnType = ColumnType;
	
	//walk through all the contacts stored in the DB
	for(hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
		hContact != NULL;
		hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0))
	{
		CallService(MS_CLIST_EXTRA_SET_ICON, (WPARAM)hContact, (LPARAM)&iec);
	}
}

/**
 * name:   ThemeDialogBackground()
 * desc:   background drawing for styled property sheets
 *
 * return:   0
 **/
static VOID ThemeDialogBackground(HWND hWnd) 
{
	if(LOBYTE(LOWORD(GetVersion())) >= 5 && LOWORD(GetVersion()) !=5) {
		static HMODULE hThemeAPI = NULL;

		if(!hThemeAPI) hThemeAPI = GetModuleHandle(_T("uxtheme"));
		if(hThemeAPI) {
			HRESULT (STDAPICALLTYPE *MyEnableThemeDialogTexture)(HWND, DWORD) = (HRESULT (STDAPICALLTYPE*)(HWND,DWORD))GetProcAddress(hThemeAPI, "EnableThemeDialogTexture");
			if(MyEnableThemeDialogTexture)
				MyEnableThemeDialogTexture(hWnd,0x00000002|0x00000004); //0x00000002|0x00000004=ETDT_ENABLETAB
		}
	}
}

/**
 * name:	SendNotify_InfoChanged()
 * desc:	send a PSN_INFOCHANGED notify to the handle
 * param:	hWnd	- handle to the window ...
 *
 * return:   0
 **/
static VOID SendNotify_InfoChanged(HWND hDlg)
{
	PSHNOTIFY pshn;

	// extended setting
	pshn.hdr.idFrom = 0;
	pshn.hdr.code = PSN_EXPERTCHANGED;
	pshn.lParam = SendMessage(GetParent(GetParent(hDlg)), PSM_ISEXPERT, NULL, NULL) ? TRUE : FALSE;
	SendMessage(hDlg, WM_NOTIFY, 0, (LPARAM)&pshn);

	// send info changed message
	pshn.hdr.code = PSN_INFOCHANGED;
	SendMessage(hDlg, WM_NOTIFY, NULL, (LPARAM)&pshn); 
}

static void FillExtraIconCombo(HWND hCombo)
{
	if(hCombo) {
		ComboBox_AddString(hCombo, TranslateT("none"));
		ComboBox_AddString(hCombo, TranslateT("Advanced #1"));
		ComboBox_AddString(hCombo, TranslateT("Advanced #2"));
		ComboBox_AddString(hCombo, TranslateT("Advanced #3"));
		ComboBox_AddString(hCombo, TranslateT("Advanced #4"));
	}
}

static BOOLEAN InitialEnableControls(HWND hDlg, const int *idCtrl, int countCtrl, BOOLEAN bEnabled)
{
	HWND hCtrl;

	while(countCtrl-- > 0) {
		hCtrl = GetDlgItem(hDlg, idCtrl[countCtrl]);
		EnableWindow(hCtrl, IsWindowEnabled(hCtrl) && bEnabled);
	}
	return bEnabled;
}

static BOOLEAN EnableControls(HWND hDlg, const int *idCtrl, int countCtrl, BOOLEAN bEnabled)
{
	while(countCtrl-- > 0) {
		EnableWindow(GetDlgItem(hDlg, idCtrl[countCtrl]), bEnabled);
	}
	return bEnabled;
}

static INT_PTR CALLBACK DlgProc_CommonOpts(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static BOOLEAN	bInitialized = 0;

	switch(uMsg) {
		
		case WM_INITDIALOG:
		{
			const int idExtraIcons[] = {
				GROUP_OPT_EXTRAICONS, TXT_OPT_GENDER, COMBO_OPT_GENDER,
				TXT_OPT_COUNTRY, COMBO_OPT_COUNTRY, TXT_OPT_DEFAULTICONS,
				CHECK_OPT_HOMEPAGEICON, CHECK_OPT_PHONEICON, CHECK_OPT_EMAILICON 
			};

			TranslateDialogDefault(hDlg);

			// init extra icons options
			if(InitialEnableControls(hDlg, idExtraIcons, SIZEOF(idExtraIcons), myGlobals.HaveCListExtraIcons)) {
				FillExtraIconCombo(GetDlgItem(hDlg, COMBO_OPT_GENDER));
			}

 			SendNotify_InfoChanged(hDlg);
			return TRUE;
		}

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code) {
				case PSN_INFOCHANGED:
				{
					const int idRadio[] = { RADIO_OPT_CM_NONE, RADIO_OPT_CM_EXIMPORT, RADIO_OPT_CM_ALL };

					bInitialized = 0;
					
					// menu item settings
					CheckDlgButton(hDlg, CHECK_OPT_CASCADEMENU, DBGetByte(SET_MENU_CASCADE, 1));
					CheckDlgButton(hDlg, CHECK_OPT_VCARDMENUITEMS, DBGetByte(SET_VCARDMENUITEMS_ENABLED, 1));
					CheckDlgButton(hDlg, idRadio[DBGetSettingRangedByte(NULL, MODNAME, SET_CONTACTMENU_CASCADE, ServiceExists(MS_CLIST_REMOVEMAINMENUITEM) ? 2 : 0, 0, 2)], TRUE);
					DlgProc_CommonOpts(hDlg, WM_COMMAND, MAKEWPARAM(CHECK_OPT_VCARDMENUITEMS, BN_CLICKED), (LPARAM)GetDlgItem(hDlg, CHECK_OPT_VCARDMENUITEMS));

					// extra icon settings
					SendDlgItemMessage(hDlg, COMBO_OPT_GENDER, CB_SETCURSEL,
						myGlobals.HaveCListExtraIcons 
							? DBGetByte(SET_CLIST_EXTRAICON_GENDER, DEFVAL_CLIST_EXTRAICON_GENDER) 
							: 0, NULL);
										
					CheckDlgButton(hDlg, CHECK_OPT_HOMEPAGEICON, DBGetByte(SET_CLIST_EXTRAICON_HOMEPAGE, DEFVAL_CLIST_EXTRAICON_HOMEPAGE));
					CheckDlgButton(hDlg, CHECK_OPT_EMAILICON, DBGetByte(SET_CLIST_EXTRAICON_EMAIL, DEFVAL_CLIST_EXTRAICON_EMAIL));
					CheckDlgButton(hDlg, CHECK_OPT_PHONEICON, DBGetByte(SET_CLIST_EXTRAICON_PHONE, DEFVAL_CLIST_EXTRAICON_PHONE));
					
					bInitialized = 1;
					break;
				}

				case PSN_APPLY: 
					// menu item settings
					{
						const int idRadio[] = { RADIO_OPT_CM_NONE, RADIO_OPT_CM_EXIMPORT, RADIO_OPT_CM_ALL };

						DBWriteContactSettingByte(NULL, MODNAME, SET_MENU_CASCADE, IsDlgButtonChecked(hDlg, CHECK_OPT_CASCADEMENU));
						DBWriteContactSettingByte(NULL, MODNAME, SET_VCARDMENUITEMS_ENABLED, IsDlgButtonChecked(hDlg, CHECK_OPT_VCARDMENUITEMS));
						for(int i = 0; i < SIZEOF(idRadio); i++) {
							if(IsDlgButtonChecked(hDlg, idRadio[i])) {
								DBWriteContactSettingByte(NULL, MODNAME, SET_CONTACTMENU_CASCADE, i);
								break;
							}
						}
						MenuItems::RebuildContact();
						MenuItems::RebuildMain();
					}

					// extra icon settings
					{
						BYTE bOldGenderColumn = DBGetByte(SET_CLIST_EXTRAICON_GENDER, DEFVAL_CLIST_EXTRAICON_GENDER);
						BYTE bNewGenderColumn = (BYTE)SendDlgItemMessage(hDlg, COMBO_OPT_GENDER, CB_GETCURSEL, NULL, NULL);
						
						// Enable/Disable extra icon modules and write new values to database
						if(bOldGenderColumn != bNewGenderColumn) {
							DBWriteContactSettingByte(NULL, MODNAME, SET_CLIST_EXTRAICON_GENDER, bNewGenderColumn);
							ClearAllExtraIcons(ColumnType(bOldGenderColumn));
							NServices::NGender::EnableExtraIcons(bNewGenderColumn);
						}

						NServices::NViewHomepage::EnableExtraIcons(IsDlgButtonChecked(hDlg, CHECK_OPT_HOMEPAGEICON), TRUE);
						NServices::NSendRecvEMail::EnableExtraIcons(IsDlgButtonChecked(hDlg, CHECK_OPT_EMAILICON), TRUE);
						NServices::NSendSMS::EnableExtraIcons(IsDlgButtonChecked(hDlg, CHECK_OPT_PHONEICON), TRUE);
					}
					break;
			}
			break;

		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case COMBO_OPT_GENDER:
					if(HIWORD(wParam) == CBN_SELCHANGE) {
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case CHECK_OPT_VCARDMENUITEMS:
				{
					const int idMenuItems[] = {
						CHECK_OPT_CASCADEMENU, TXT_OPT_CONTACTMENU, RADIO_OPT_CM_NONE,
						RADIO_OPT_CM_EXIMPORT, RADIO_OPT_CM_ALL };
					EnableControls(hDlg, idMenuItems, SIZEOF(idMenuItems), Button_GetCheck((HWND)lParam) && ServiceExists(MS_CLIST_REMOVEMAINMENUITEM));
				}
				case RADIO_OPT_CM_NONE:
				case RADIO_OPT_CM_EXIMPORT:
				case RADIO_OPT_CM_ALL:
				case CHECK_OPT_CASCADEMENU:
				case CHECK_OPT_HOMEPAGEICON:
				case CHECK_OPT_EMAILICON:
				case CHECK_OPT_PHONEICON:
					if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
			}
			break;
	}
	return FALSE;
}

static INT_PTR CALLBACK DlgProc_AdvancedOpts(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static BOOLEAN	bInitialized = 0;

	switch(uMsg) {
		
		case WM_INITDIALOG:
		{
			TranslateDialogDefault(hDlg);

			EnableWindow(GetDlgItem(hDlg, CHECK_OPT_METACPY), myGlobals.HaveMetaContacts);
			EnableWindow(GetDlgItem(hDlg, CHECK_OPT_METASCAN), myGlobals.HaveMetaContacts);

			SendNotify_InfoChanged(hDlg);
			return TRUE;
		}

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code) {
				case PSN_INFOCHANGED:
				{
					bInitialized = 0;

					CheckDlgButton(hDlg, CHECK_OPT_ICOVERSION, DBGetByte(SET_ICONS_CHECKFILEVERSION, 1));
					CheckDlgButton(hDlg, CHECK_OPT_BUTTONICONS, DBGetByte(SET_ICONS_BUTTONS, 1));
					CheckDlgButton(hDlg, CHECK_OPT_METACPY, DBGetByte(SET_META_COPYDATA, FALSE));
					CheckDlgButton(hDlg, CHECK_OPT_METASCAN, DBGetByte(SET_META_SCAN, TRUE));
					CheckDlgButton(hDlg, CHECK_OPT_GETCONTACTINFO_ENABLED, DBGetByte(SET_GETCONTACTINFO_ENABLED, DEFVAL_GETCONTACTINFO_ENABLED));

					bInitialized = 1;
					break;
				}

				case PSN_APPLY:
				{
					DBWriteContactSettingByte(NULL, MODNAME, SET_ICONS_CHECKFILEVERSION, IsDlgButtonChecked(hDlg, CHECK_OPT_ICOVERSION));
					DBWriteContactSettingByte(NULL, MODNAME, SET_ICONS_BUTTONS, IsDlgButtonChecked(hDlg, CHECK_OPT_BUTTONICONS));
					DBWriteContactSettingByte(NULL, MODNAME, SET_META_COPYDATA, IsDlgButtonChecked(hDlg, CHECK_OPT_METACPY));
					DBWriteContactSettingByte(NULL, MODNAME, SET_META_SCAN, IsDlgButtonChecked(hDlg, CHECK_OPT_METASCAN));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_GETCONTACTINFO_ENABLED, IsDlgButtonChecked(hDlg, CHECK_OPT_GETCONTACTINFO_ENABLED));
					break;
				}
			}
			break;

		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case CHECK_OPT_ICOVERSION:
				case CHECK_OPT_BUTTONICONS:
				case CHECK_OPT_METACPY:
				case CHECK_OPT_METASCAN:
				case CHECK_OPT_GETCONTACTINFO_ENABLED:
					if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
				case BTN_OPT_RESET:
				{
					HANDLE hContact;
					ENUMLIST il;
					ENUMLISTITEM *pil;

					if(IDYES != MsgBox(hDlg, MB_ICON_WARNING|MB_YESNO, _T("Question"), _T("Reset factory defaults"), _T("This will delete all settings, you've made!\nAll TreeView settings, window positions and any other settings!\n\nAre you sure to procceed?")))
						break;

					// delete global settings
					DBDeleteModule(NULL, USERINFO"Ex");					
					DBDeleteModule(NULL, USERINFO"ExW");

					// delete all iconsettings
					if(DBEnumSettings(NULL, "SkinIcons", &il)) {
						for(pil = il.first; pil != NULL; pil = pil->next) {
							if(pil->pszName && !strncmp(pil->pszName, "UserInfoEx", 10))
								DBDeleteContactSetting(NULL, "SkinIcons", pil->pszName);
						}
						DBFreeEnumList(&il);
					}
					
					// delete old contactsettings
					for(hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
						hContact != NULL && hContact != INVALID_HANDLE_VALUE;
						hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0))
					{
						DBDeleteContactSetting(hContact, USERINFO, "PListColWidth0");
						DBDeleteContactSetting(hContact, USERINFO, "PListColWidth1");
						DBDeleteContactSetting(hContact, USERINFO, "PListColWidth2");
						DBDeleteContactSetting(hContact, USERINFO, "EMListColWidth0");
						DBDeleteContactSetting(hContact, USERINFO, "EMListColWidth1");
						DBDeleteContactSetting(hContact, USERINFO, "BirthRemind");
						DBDeleteContactSetting(hContact, USERINFO, "RemindBirthday");
						DBDeleteContactSetting(hContact, USERINFO, "RemindDaysErlier");
						DBDeleteContactSetting(hContact, USERINFO, "vCardPath");

						DBDeleteModule(hContact, USERINFO"Ex");					
						DBDeleteModule(hContact, USERINFO"ExW");					
					}

					SendMessage(GetParent(hDlg), PSM_FORCECHANGED, NULL, NULL);
					MsgBox(hDlg, MB_ICON_INFO, _T("Ready"), _T("Everything is done!"), _T("All settings are reset to default values now!"));
					break;
				}
			}
			break;
	}
	return FALSE;
}

static INT_PTR CALLBACK DlgProc_DetailsDlgOpts(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static BOOLEAN	bInitialized = 0;

	switch(uMsg) {
		
		case WM_INITDIALOG:
		{
			TranslateDialogDefault(hDlg);
			SendNotify_InfoChanged(hDlg);
			return TRUE;
		}

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code) {
				case PSN_INFOCHANGED:
				{
					bInitialized = 0;
					// init colors
					CheckDlgButton(hDlg, CHECK_OPT_CLR, DBGetByte(SET_PROPSHEET_SHOWCOLOURS, 1));
					SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(CHECK_OPT_CLR, BN_CLICKED), (LPARAM)GetDlgItem(hDlg, CHECK_OPT_CLR));
					SendDlgItemMessage(hDlg, CLR_NORMAL, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRNORMAL, RGB(90, 90, 90)));
					SendDlgItemMessage(hDlg, CLR_USER, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRCUSTOM, RGB(0, 10, 130)));
					SendDlgItemMessage(hDlg, CLR_BOTH, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRBOTH, RGB(0, 160, 10)));
					SendDlgItemMessage(hDlg, CLR_CHANGED, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRCHANGED, RGB(190, 0, 0)));
					SendDlgItemMessage(hDlg, CLR_META, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRMETA, RGB(120, 40, 130)));

					// treeview options
					CheckDlgButton(hDlg, CHECK_OPT_GROUPS, DBGetByte(SET_PROPSHEET_GROUPS, 1));
					CheckDlgButton(hDlg, CHECK_OPT_SORTTREE, DBGetByte(SET_PROPSHEET_SORTITEMS, 0));
					CheckDlgButton(hDlg, CHECK_OPT_READONLYLABEL, DBGetByte(SET_PROPSHEET_READONLYLABEL, 0));

					// common options
					CheckDlgButton(hDlg, CHECK_OPT_READONLY, DBGetByte(SET_PROPSHEET_PCBIREADONLY, 0));
					Button_Enable(GetDlgItem(hDlg, CHECK_OPT_CHANGEMYDETAILS), myGlobals.CanChangeDetails);
					CheckDlgButton(hDlg, CHECK_OPT_CHANGEMYDETAILS, DBGetByte(SET_PROPSHEET_CHANGEMYDETAILS, 0));
					bInitialized = 1;
					break;
				}

				case PSN_APPLY:
				{
					DBWriteContactSettingByte(NULL, MODNAME, SET_PROPSHEET_SHOWCOLOURS, IsDlgButtonChecked(hDlg, CHECK_OPT_CLR));
					DBWriteContactSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRNORMAL, SendDlgItemMessage(hDlg, CLR_NORMAL, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRCUSTOM, SendDlgItemMessage(hDlg, CLR_USER, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRBOTH, SendDlgItemMessage(hDlg, CLR_BOTH, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRCHANGED, SendDlgItemMessage(hDlg, CLR_CHANGED, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingDword(NULL, MODNAME, SET_PROPSHEET_CLRMETA, SendDlgItemMessage(hDlg, CLR_META, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingByte(NULL, MODNAME, SET_PROPSHEET_GROUPS, IsDlgButtonChecked(hDlg, CHECK_OPT_GROUPS));
					DBWriteContactSettingByte(NULL, MODNAME, SET_PROPSHEET_SORTITEMS, IsDlgButtonChecked(hDlg, CHECK_OPT_SORTTREE));
					DBWriteContactSettingByte(NULL, MODNAME, SET_PROPSHEET_PCBIREADONLY, IsDlgButtonChecked(hDlg, CHECK_OPT_READONLY));
					DBWriteContactSettingByte(NULL, MODNAME, SET_PROPSHEET_READONLYLABEL, IsDlgButtonChecked(hDlg, CHECK_OPT_READONLYLABEL));
					DBWriteContactSettingByte(NULL, MODNAME, SET_PROPSHEET_CHANGEMYDETAILS, IsDlgButtonChecked(hDlg, CHECK_OPT_CHANGEMYDETAILS));
					break;
				}
			}
			break;

		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case CHECK_OPT_CLR:
					if(HIWORD(wParam) == BN_CLICKED) {
						BOOL bChecked = SendMessage((HWND)lParam, BM_GETCHECK, NULL, NULL);

						EnableWindow(GetDlgItem(hDlg, CLR_NORMAL), bChecked);
						EnableWindow(GetDlgItem(hDlg, CLR_USER), bChecked);
						EnableWindow(GetDlgItem(hDlg, CLR_BOTH), bChecked);
						EnableWindow(GetDlgItem(hDlg, CLR_CHANGED), bChecked);
						EnableWindow(GetDlgItem(hDlg, CLR_META), bChecked);
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_CLR_NORMAL), bChecked);
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_CLR_USER), bChecked);
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_CLR_BOTH), bChecked);
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_CLR_CHANGED), bChecked);
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_CLR_META), bChecked);
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case CHECK_OPT_GROUPS:
				case CHECK_OPT_SORTTREE:
				case CHECK_OPT_READONLYLABEL:
				case CHECK_OPT_READONLY:
				case CHECK_OPT_CHANGEMYDETAILS:
				case CHECK_OPT_VCARDMENUITEMS:
					if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
				default:
					if(bInitialized && HIWORD(wParam) == CPN_COLOURCHANGED)
						SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
			}
			break;
	}
	return FALSE;
}


static INT_PTR CALLBACK DlgProc_ReminderOpts(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static BOOLEAN	bInitialized = 0;

	switch(uMsg) {
		
		case WM_INITDIALOG:
		{
			HWND hCtrl;

			TranslateDialogDefault(hDlg);

			SendDlgItemMessage(hDlg, ICO_BIRTHDAY, STM_SETIMAGE, IMAGE_ICON, (LPARAM)NIcoLib::GetIcon(ICO_DLG_ANNIVERSARY));

			// set colours			
			SendDlgItemMessage(hDlg, EDIT_REMIND, EM_LIMITTEXT, 2, 0);
			SendDlgItemMessage(hDlg, SPIN_REMIND, UDM_SETRANGE32, 0, 50);
			SendDlgItemMessage(hDlg, EDIT_REMIND2, EM_LIMITTEXT, 5, 0);
			SendDlgItemMessage(hDlg, SPIN_REMIND2, UDM_SETRANGE32, 0, 65535);
			SendDlgItemMessage(hDlg, EDIT_REMIND_SOUNDOFFSET, EM_LIMITTEXT, 2, 0);
			SendDlgItemMessage(hDlg, SPIN_REMIND_SOUNDOFFSET, UDM_SETRANGE32, 0, 50);

			FillExtraIconCombo(GetDlgItem(hDlg, EDIT_EXTRAICON));

			if(hCtrl = GetDlgItem(hDlg, EDIT_REMIND_ENABLED)) {
				ComboBox_AddString(hCtrl, TranslateT("Reminder disabled"));
				ComboBox_AddString(hCtrl, TranslateT("Anniversaries only"));
				ComboBox_AddString(hCtrl, TranslateT("Bithdays only"));
				ComboBox_AddString(hCtrl, TranslateT("everything"));
			}
			if(hCtrl = GetDlgItem(hDlg, EDIT_BIRTHMODULE)) {
				ComboBox_AddString(hCtrl, TranslateT("mBirthday (default)"));
				ComboBox_AddString(hCtrl, TranslateT("UserInfo"));
			}
			SendNotify_InfoChanged(hDlg);
			return TRUE;
		}

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code) {
				case PSN_INFOCHANGED:
				{
					bInitialized = 0;
					BOOLEAN bEnabled;

					// set reminder options
					bEnabled = DBGetByte(SET_REMIND_ENABLED, DEFVAL_REMIND_ENABLED);
					SendDlgItemMessage(hDlg, EDIT_REMIND_ENABLED, CB_SETCURSEL, bEnabled, NULL);
					DlgProc_ReminderOpts(hDlg, WM_COMMAND, MAKEWPARAM(EDIT_REMIND_ENABLED, CBN_SELCHANGE), (LPARAM)GetDlgItem(hDlg, EDIT_REMIND_ENABLED));
					
					CheckDlgButton(hDlg, CHECK_REMIND_MI, DBGetByte(SET_REMIND_MENUENABLED, DEFVAL_REMIND_MENUENABLED));
					CheckDlgButton(hDlg, CHECK_REMIND_FLASHICON, DBGetByte(SET_REMIND_FLASHICON, FALSE));
					CheckDlgButton(hDlg, CHECK_REMIND_VISIBLEONLY, DBGetByte(SET_REMIND_CHECKVISIBLE, DEFVAL_REMIND_CHECKVISIBLE));
					CheckDlgButton(hDlg, CHECK_REMIND_STARTUP, DBGetByte(SET_REMIND_CHECKON_STARTUP, FALSE));
					CheckDlgButton(hDlg, CHECK_REMIND_SECURED, DBGetByte(SET_REMIND_SECUREBIRTHDAY, FALSE));

					SetDlgItemInt(hDlg, EDIT_REMIND, DBGetSettingWord(NULL, MODNAME, SET_REMIND_OFFSET, DEFVAL_REMIND_OFFSET), FALSE);
					SetDlgItemInt(hDlg, EDIT_REMIND_SOUNDOFFSET, DBGetByte(SET_REMIND_SOUNDOFFSET, DEFVAL_REMIND_SOUNDOFFSET), FALSE);
					SetDlgItemInt(hDlg, EDIT_REMIND2, DBGetSettingWord(NULL, MODNAME, SET_REMIND_NOTIFYINTERVAL, DEFVAL_REMIND_NOTIFYINTERVAL), FALSE);
					SendDlgItemMessage(hDlg, EDIT_EXTRAICON, CB_SETCURSEL, myGlobals.HaveCListExtraIcons ? DBGetByte(SET_REMIND_EXTRAICON, 1) : 0, NULL);
					
					SendDlgItemMessage(hDlg, EDIT_BIRTHMODULE, CB_SETCURSEL, DBGetByte(SET_REMIND_BIRTHMODULE, DEFVAL_REMIND_BIRTHMODULE), NULL);
					{
						MTime mtLast;
						TCHAR szTime[MAX_PATH];

						mtLast.DBGetStamp(NULL, MODNAME, SET_REMIND_LASTCHECK);
						mtLast.UTCToLocal();
						mtLast.TimeFormat(szTime, SIZEOF(szTime));

						SetDlgItemText(hDlg, TXT_REMIND_LASTCHECK, szTime);
					}
					
					bInitialized = 1;
					break;
				}

				case PSN_APPLY:
				{
					BOOL bTranslated;
					UINT iVal;
					BYTE bColumn, bNewVal;
					BOOLEAN bReminderCheck = FALSE;

					// save checkbox options
					DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_MENUENABLED, IsDlgButtonChecked(hDlg, CHECK_REMIND_MI));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_FLASHICON, IsDlgButtonChecked(hDlg, CHECK_REMIND_FLASHICON));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_CHECKVISIBLE, IsDlgButtonChecked(hDlg, CHECK_REMIND_VISIBLEONLY));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_CHECKON_STARTUP, IsDlgButtonChecked(hDlg, CHECK_REMIND_STARTUP));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_SECUREBIRTHDAY, IsDlgButtonChecked(hDlg, CHECK_REMIND_SECURED));

					// save offset
					iVal = GetDlgItemInt(hDlg, EDIT_REMIND, &bTranslated, FALSE);
					if(bTranslated && iVal != DBGetSettingWord(NULL, MODNAME, SET_REMIND_OFFSET, DEFVAL_REMIND_OFFSET)) {
						DBWriteContactSettingWord(NULL, MODNAME, SET_REMIND_OFFSET, (BYTE)iVal);
						bReminderCheck = TRUE;
					}

					iVal = GetDlgItemInt(hDlg, EDIT_REMIND_SOUNDOFFSET, &bTranslated, FALSE);
					if(bTranslated) DBWriteContactSettingByte(NULL, MODNAME, SET_REMIND_SOUNDOFFSET, (BYTE)iVal);

					// save notification interval
					iVal = GetDlgItemInt(hDlg, EDIT_REMIND2, &bTranslated, FALSE);
					if(bTranslated)	DBWriteContactSettingWord(NULL, MODNAME, SET_REMIND_NOTIFYINTERVAL, (WORD)iVal);
	
					// save primary birthday module
					bNewVal = (BYTE)SendDlgItemMessage(hDlg, EDIT_BIRTHMODULE, CB_GETCURSEL, NULL, NULL);
					DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_BIRTHMODULE, bNewVal);

					// save new clist extra icon
					bColumn = DBGetByte(SET_REMIND_EXTRAICON, 1);
					bNewVal = (BYTE)SendDlgItemMessage(hDlg, EDIT_EXTRAICON, CB_GETCURSEL, NULL, NULL);
					if(bColumn != bNewVal) {
						ClearAllExtraIcons(ColumnType(bColumn));
						DBWriteContactSettingByte(NULL, MODNAME,  SET_REMIND_EXTRAICON, bNewVal);
						bReminderCheck = TRUE;
					}

					// update current reminder state
					bNewVal = (BYTE)SendDlgItemMessage(hDlg, EDIT_REMIND_ENABLED, CB_GETCURSEL, NULL, NULL);
					if(DBGetByte(SET_REMIND_ENABLED, 1) != bNewVal) {
						DBWriteContactSettingByte(NULL, MODNAME, SET_REMIND_ENABLED, bNewVal);
						if(bNewVal == NReminder::REMIND_OFF) {
							ClearAllExtraIcons(ColumnType(bColumn));
							NReminder::Enable(FALSE);
						}
						else {
							NReminder::Enable(TRUE);
							bReminderCheck = TRUE;
						}
					}
					// update all contact list extra icons
					if(bReminderCheck) {
						NReminder::CheckAll(NReminder::NOTIFY_CLIST);
					}
					break;
				}
			}
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case EDIT_REMIND_ENABLED:
					if(HIWORD(wParam) == CBN_SELCHANGE) {
						INT i, bEnabled = ComboBox_GetCurSel((HWND)lParam) > 0;
						const WORD idCtrl[] = {
							CHECK_REMIND_MI, EDIT_REMIND, EDIT_REMIND2, SPIN_REMIND, SPIN_REMIND2, TXT_REMIND,
							TXT_REMIND2, TXT_REMIND3, TXT_REMIND4, TXT_REMIND6, TXT_REMIND7, TXT_REMIND8, TXT_REMIND9,
							TXT_REMIND_LASTCHECK, CHECK_REMIND_FLASHICON, EDIT_BIRTHMODULE, CHECK_REMIND_VISIBLEONLY,
							CHECK_REMIND_SECURED, CHECK_REMIND_STARTUP, EDIT_REMIND_SOUNDOFFSET, SPIN_REMIND_SOUNDOFFSET
						};
						
						for(i = 0; i < SIZEOF(idCtrl); i++) {
							EnableWindow(GetDlgItem(hDlg, idCtrl[i]), bEnabled);
						}
						bEnabled &= myGlobals.HaveCListExtraIcons;
						EnableWindow(GetDlgItem(hDlg, TXT_REMIND5), bEnabled);
						EnableWindow(GetDlgItem(hDlg, EDIT_EXTRAICON), bEnabled);

						HWND hParent = GetParent(hDlg);
						if(hParent) {
							SendMessage(hParent, PSM_ENABLE_TABITEM, IDD_OPT_POPUP, bEnabled && ServiceExists(MS_POPUP_ADDPOPUP));
							if(bInitialized)
								SendMessage(hParent, PSM_CHANGED, 0, 0);
						}
					}
					break;
				case EDIT_EXTRAICON:
				case EDIT_BIRTHMODULE:
					if(HIWORD(wParam) == CBN_SELCHANGE) {
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case CHECK_REMIND_MI:
				case CHECK_REMIND_FLASHICON:
				case CHECK_REMIND_VISIBLEONLY:
				case CHECK_REMIND_STARTUP:
				case CHECK_REMIND_SECURED:
					if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
				case EDIT_REMIND:
					if(bInitialized && HIWORD(wParam) == EN_UPDATE) {
						BOOL bTranslated;
                        UINT iVal;
						
						iVal = GetDlgItemInt(hDlg, LOWORD(wParam), &bTranslated, FALSE);
						if(bTranslated && (iVal != DBGetByte(SET_REMIND_OFFSET, DEFVAL_REMIND_OFFSET)))
							SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case EDIT_REMIND2:
					if(bInitialized && HIWORD(wParam) == EN_UPDATE) {
						BOOL bTranslated;
                        UINT iVal;
						
						iVal = GetDlgItemInt(hDlg, LOWORD(wParam), &bTranslated, FALSE);
						if(bTranslated && (iVal != DBGetSettingWord(NULL, MODNAME, SET_REMIND_NOTIFYINTERVAL, DEFVAL_REMIND_NOTIFYINTERVAL)))
							SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
			}
			break;
	}
	return FALSE;
}

static INT_PTR CALLBACK DlgProc_Popups(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static BOOLEAN	bInitialized = 0;

	switch(uMsg) {		
		case WM_INITDIALOG:
		{
			TranslateDialogDefault(hDlg);
			SendNotify_InfoChanged(hDlg);
			return TRUE;
		}
		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code) {
				case PSN_INFOCHANGED:
				{
					BYTE bDelay, isEnabled;

					bInitialized = 0;
					// enable/disable popups
					isEnabled = DBGetByte(SET_POPUP_ENABLED, DEFVAL_POPUP_ENABLED);
					CheckDlgButton(hDlg, CHECK_OPT_POPUP_ENABLED, isEnabled);
					SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(CHECK_OPT_POPUP_ENABLED, BN_CLICKED), (LPARAM)GetDlgItem(hDlg, CHECK_OPT_POPUP_ENABLED));

					// set colortype checkboxes and color controls
					SendDlgItemMessage(hDlg, CLR_BBACK, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLOR_BACK, RGB(192,180,30)));
					SendDlgItemMessage(hDlg, CLR_BTEXT, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLOR_TEXT, RGB(0, 0, 0)));
					switch(DBGetByte(SET_POPUP_BIRTHDAY_COLORTYPE, POPUP_COLOR_CUSTOM)) {
						case POPUP_COLOR_DEFAULT:
							CheckDlgButton(hDlg, CHECK_OPT_POPUP_DEFCLR, TRUE);
							break;
						case POPUP_COLOR_WINDOWS:
							CheckDlgButton(hDlg, CHECK_OPT_POPUP_WINCLR, TRUE);
							break;
					}
					SendDlgItemMessage(hDlg, CLR_ABACK, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLOR_BACK, RGB(90, 190, 130)));
					SendDlgItemMessage(hDlg, CLR_ATEXT, CPM_SETCOLOUR, 0, DBGetSettingDword(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLOR_TEXT, RGB(0, 0, 0)));
					switch(DBGetByte(SET_POPUP_ANNIVERSARY_COLORTYPE, POPUP_COLOR_CUSTOM)) {
						case POPUP_COLOR_DEFAULT:
							CheckDlgButton(hDlg, CHECK_OPT_POPUP_ADEFCLR, TRUE);
							break;
						case POPUP_COLOR_WINDOWS:
							CheckDlgButton(hDlg, CHECK_OPT_POPUP_AWINCLR, TRUE);
							break;
					}
					
					if(isEnabled) {
						SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(CHECK_OPT_POPUP_DEFCLR, BN_CLICKED), NULL);
						SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(CHECK_OPT_POPUP_ADEFCLR, BN_CLICKED), NULL);
					}
					// set delay values
					bDelay = DBGetByte(SET_POPUP_DELAY, 0);
					switch(bDelay) {
						case 0:
							CheckDlgButton(hDlg, RADIO_OPT_POPUP_DEFAULT, TRUE);
							if(isEnabled) EnableWindow(GetDlgItem(hDlg, EDIT_DELAY), FALSE);
							break;
						case 255:
							CheckDlgButton(hDlg, RADIO_OPT_POPUP_PERMANENT, TRUE);
							if(isEnabled) EnableWindow(GetDlgItem(hDlg, EDIT_DELAY), FALSE);
							break;
						default:
						{
							CheckDlgButton(hDlg, RADIO_OPT_POPUP_CUSTOM, TRUE);
							SetDlgItemInt(hDlg, EDIT_DELAY, bDelay, FALSE);
							break;
						}
					}
					bInitialized = TRUE;
					break;
				}
				case PSN_APPLY:
				{
					DBWriteContactSettingByte(NULL, MODNAME,  SET_POPUP_ENABLED, IsDlgButtonChecked(hDlg, CHECK_OPT_POPUP_ENABLED));

					// save popup style for birthdays
					DBWriteContactSettingDword(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLOR_BACK, SendDlgItemMessage(hDlg, CLR_BBACK, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingDword(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLOR_TEXT, SendDlgItemMessage(hDlg, CLR_BTEXT, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_POPUP_BIRTHDAY_COLORTYPE, 
						SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_DEFCLR, BM_GETCHECK, NULL, NULL) 
							? POPUP_COLOR_DEFAULT 
							: SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_WINCLR, BM_GETCHECK, NULL, NULL) 
								? POPUP_COLOR_WINDOWS 
								: POPUP_COLOR_CUSTOM);

					// save popup style for anniversaries
					DBWriteContactSettingDword(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLOR_BACK, SendDlgItemMessage(hDlg, CLR_ABACK, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingDword(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLOR_TEXT, SendDlgItemMessage(hDlg, CLR_ATEXT, CPM_GETCOLOUR, 0, 0));
					DBWriteContactSettingByte(NULL, MODNAME,  SET_POPUP_ANNIVERSARY_COLORTYPE, 
						SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_ADEFCLR, BM_GETCHECK, NULL, NULL) 
							? POPUP_COLOR_DEFAULT 
							: SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_AWINCLR, BM_GETCHECK, NULL, NULL) 
								? POPUP_COLOR_WINDOWS 
								: POPUP_COLOR_CUSTOM);

					// save delay
					if(SendDlgItemMessage(hDlg, RADIO_OPT_POPUP_PERMANENT, BM_GETCHECK, NULL, NULL))
						DBWriteContactSettingByte(NULL, MODNAME, SET_POPUP_DELAY, 255);
					else
					if(SendDlgItemMessage(hDlg, RADIO_OPT_POPUP_CUSTOM, BM_GETCHECK, NULL, NULL)) {
						TCHAR szDelay[4];
						GetDlgItemText(hDlg, EDIT_DELAY, szDelay, sizeof(szDelay));
						DBWriteContactSettingByte(NULL, MODNAME, SET_POPUP_DELAY, (BYTE)_tcstol(szDelay, NULL, 10));
					}
					else
						DBDeleteContactSetting(NULL, MODNAME, SET_POPUP_DELAY);
					break;
				}
			}
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case CHECK_OPT_POPUP_ENABLED:
					if(HIWORD(wParam) == BN_CLICKED) {
						INT i, bEnabled = SendMessage((HWND)lParam, BM_GETCHECK, NULL, NULL);
						const WORD idCtrl[] = { 
							CHECK_OPT_POPUP_DEFCLR, CHECK_OPT_POPUP_WINCLR, 
							CLR_BBACK, TXT_OPT_POPUP_CLR_BACK, 
							CLR_BTEXT, TXT_OPT_POPUP_CLR_TEXT,
							CHECK_OPT_POPUP_ADEFCLR, CHECK_OPT_POPUP_AWINCLR,
							CLR_ABACK, TXT_OPT_POPUP_CLR_ABACK,
							CLR_ATEXT, TXT_OPT_POPUP_CLR_ATEXT,
							RADIO_OPT_POPUP_DEFAULT, RADIO_OPT_POPUP_CUSTOM, 
							RADIO_OPT_POPUP_PERMANENT, EDIT_DELAY
						};
						for(i = 0; i < SIZEOF(idCtrl); i++) {
							EnableWindow(GetDlgItem(hDlg, idCtrl[i]), bEnabled);
						}

						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;

				case CHECK_OPT_POPUP_DEFCLR:
				case CHECK_OPT_POPUP_WINCLR:
					if(HIWORD(wParam) == BN_CLICKED) {
						INT bDefClr = SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_DEFCLR, BM_GETCHECK, NULL, NULL);
						INT bWinClr = SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_WINCLR, BM_GETCHECK, NULL, NULL);

						EnableWindow(GetDlgItem(hDlg, CHECK_OPT_POPUP_DEFCLR), !bWinClr);
						EnableWindow(GetDlgItem(hDlg, CHECK_OPT_POPUP_WINCLR), !bDefClr);
						EnableWindow(GetDlgItem(hDlg, CLR_BBACK), !(bDefClr || bWinClr));
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_POPUP_CLR_BACK), !(bDefClr || bWinClr));
						EnableWindow(GetDlgItem(hDlg, CLR_BTEXT), !(bDefClr || bWinClr));
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_POPUP_CLR_TEXT), !(bDefClr || bWinClr));
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;

				case CHECK_OPT_POPUP_ADEFCLR:
				case CHECK_OPT_POPUP_AWINCLR:
					if(HIWORD(wParam) == BN_CLICKED) {
						INT bDefClr = SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_ADEFCLR, BM_GETCHECK, NULL, NULL);
						INT bWinClr = SendDlgItemMessage(hDlg, CHECK_OPT_POPUP_AWINCLR, BM_GETCHECK, NULL, NULL);

						EnableWindow(GetDlgItem(hDlg, CHECK_OPT_POPUP_ADEFCLR), !bWinClr);
						EnableWindow(GetDlgItem(hDlg, CHECK_OPT_POPUP_AWINCLR), !bDefClr);
						EnableWindow(GetDlgItem(hDlg, CLR_ABACK), !(bDefClr || bWinClr));
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_POPUP_CLR_ABACK), !(bDefClr || bWinClr));
						EnableWindow(GetDlgItem(hDlg, CLR_ATEXT), !(bDefClr || bWinClr));
						EnableWindow(GetDlgItem(hDlg, TXT_OPT_POPUP_CLR_ATEXT), !(bDefClr || bWinClr));
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;

				case RADIO_OPT_POPUP_DEFAULT:
					if(HIWORD(wParam) == BN_CLICKED) {
						EnableWindow(GetDlgItem(hDlg, EDIT_DELAY), FALSE);
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case RADIO_OPT_POPUP_CUSTOM:
					if(HIWORD(wParam) == BN_CLICKED) {
						EnableWindow(GetDlgItem(hDlg, EDIT_DELAY), TRUE);
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case RADIO_OPT_POPUP_PERMANENT:
					if(HIWORD(wParam) == BN_CLICKED) {
						EnableWindow(GetDlgItem(hDlg, EDIT_DELAY), FALSE);
						if(bInitialized) SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					}
					break;
				case EDIT_DELAY:
					if(bInitialized && HIWORD(wParam) == EN_UPDATE)
						SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
				default:
					if(bInitialized && HIWORD(wParam) == CPN_COLOURCHANGED)
						SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
					break;
			}
			break;
	}
	return FALSE;
}

/*
============================================================================================
	external interface
============================================================================================
*/

/**
 * name:	OnInit()
 * desc:	initializes the option page for our module, if user opens the option dialog page.
 *
 * return:	0 or 1
 **/
static INT OnInit(WPARAM wParam, LPARAM lParam)
{
	OPTIONSDIALOGPAGE odp = {0};
	
	NContactDetailsPS::InitTreeIcons();
	
	odp.position = 95400;
	odp.hInstance = ghInst;
	odp.ptszTitle = _T("Extended UserInfo");
	odp.ptszGroup = _T("Plugins");
	odp.cbSize = sizeof(odp);

	// Common page
	odp.ptszTab = _T("Common");
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_COMMON);
	odp.pfnDlgProc = DlgProc_CommonOpts;
	odp.flags = ODPF_BOLDGROUPS|ODPF_TCHAR;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);

	// Advanced page
	odp.ptszTab = _T("Advanced");
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_ADVANCED);
	odp.pfnDlgProc = DlgProc_AdvancedOpts;
	odp.flags = ODPF_BOLDGROUPS|ODPF_TCHAR|ODPF_EXPERTONLY;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);

	// Details Dialog page
	odp.ptszTab = _T("Details Dialog");
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_DETAILSDLG);
	odp.pfnDlgProc = DlgProc_DetailsDlgOpts;
	odp.flags = ODPF_BOLDGROUPS|ODPF_TCHAR;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);

	// Reminder page
	odp.ptszTab = _T("Reminder");
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_REMINDER);
	odp.pfnDlgProc = DlgProc_ReminderOpts;
	odp.flags = ODPF_BOLDGROUPS|ODPF_TCHAR;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);

	// Popups page
	if(ServiceExists(MS_POPUP_ADDPOPUP)) {
		odp.ptszTab = _T("Popups");
		odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_POPUP);
		odp.pfnDlgProc = DlgProc_Popups;
		odp.flags = ODPF_BOLDGROUPS|ODPF_TCHAR|ODPF_EXPERTONLY;
		CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);
	}
	return 0;
}

VOID LoadModule()
{
	HookEvent(ME_OPT_INITIALISE, OnInit);
}


} // namespace options